package cn.iocoder.yudao.module.ai.controller.admin.algorithm;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.ai.utils.AssertUtil;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


import cn.iocoder.yudao.module.ai.controller.admin.algorithm.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.algorithmlibrary.AlgorithmLibraryDO;
import cn.iocoder.yudao.module.ai.service.algorithmlibrary.AlgorithmLibraryService;

@Tag(name = "管理后台 - 推理算法库")
@RestController
@RequestMapping("/ai/algorithm-library")
@Validated
public class AlgorithmLibraryController {

    @Resource
    private AlgorithmLibraryService algorithmLibraryService;
    @Resource
    private DeptApi deptApi;

    /**
     * 创建推理算法库：
     *    1.创建训练模型，供应商为部门id
     *    2.除平台管理员外，数据权限仅本部门可见。
     *    3.数据权限配置类：ModelDataPermissionConfiguration
     * @param createReqVO
     * @return
     */
    @PostMapping("/create")
    @Operation(summary = "创建推理算法库")
    @PreAuthorize("@ss.hasPermission('ai:algorithm-library:create')")
    public CommonResult<Long> createAlgorithmLibrary(@Valid @RequestBody AlgorithmLibrarySaveReqVO createReqVO) {
        createReqVO.setSuplierId(deptApi.queryFirstDept(SecurityFrameworkUtils.getLoginUserDeptId()));
        return success(algorithmLibraryService.createAlgorithmLibrary(createReqVO));
    }


    /**
     * 更新训练模型：
     *     1.校验权限仅创建者本人才能修改
     * @param updateReqVO
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "更新推理算法库")
    @PreAuthorize("@ss.hasPermission('ai:algorithm-library:update')")
    public CommonResult<Boolean> updateAlgorithmLibrary(@Valid @RequestBody AlgorithmLibrarySaveReqVO updateReqVO) {
        AlgorithmLibraryDO algorithmLibrary= algorithmLibraryService.getAlgorithmLibrary(updateReqVO.getId());
        AssertUtil.notEqual(SecurityFrameworkUtils.getLoginUserId(),algorithmLibrary.getCreator(),"仅允许模型开发者修改模型。");
        algorithmLibraryService.updateAlgorithmLibrary(updateReqVO);
        return success(true);
    }


    /**
     * 删除训练模型：
     *     1.校验权限仅创建者本人才能删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除推理算法库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:algorithm-library:delete')")
    public CommonResult<Boolean> deleteAlgorithmLibrary(@RequestParam("id") Long id) {
        AlgorithmLibraryDO algorithmLibrary= algorithmLibraryService.getAlgorithmLibrary(id);
        AssertUtil.notEqual(SecurityFrameworkUtils.getLoginUserId(),algorithmLibrary.getCreator(),"仅允许模型开发者删除模型。");
        algorithmLibraryService.deleteAlgorithmLibrary(id);
        return success(true);
    }

    /**
     * 查询单条训练模型记录：
     * @param id
     * @return
     */
    @GetMapping("/get")
    @Operation(summary = "获得推理算法库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:algorithm-library:query')")
    public CommonResult<AlgorithmLibraryRespVO> getAlgorithmLibrary(@RequestParam("id") Long id) {
        AlgorithmLibraryDO algorithmLibrary = algorithmLibraryService.getAlgorithmLibrary(id);
        return success(BeanUtils.toBean(algorithmLibrary, AlgorithmLibraryRespVO.class));
    }

    /**
     * 分页获得推理算法清单：
     * @param pageReqVO
     * @return
     */
    @GetMapping("/page")
    @Operation(summary = "分页获得推理算法清单")
    @PreAuthorize("@ss.hasPermission('ai:algorithm-library:query')")
    public CommonResult<PageResult<AlgorithmLibraryRespVO>> getAlgorithmLibraryPage(@Valid AlgorithmLibraryPageReqVO pageReqVO) {
        PageResult<AlgorithmLibraryDO> pageResult = algorithmLibraryService.getAlgorithmLibraryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AlgorithmLibraryRespVO.class));
    }


}